import request from './request.js'

// ==================== 用户认证 ====================

// 用户登录
export function login(data) {
  return request({
    url: '/Users/login',
    method: 'post',
    data,
    withCredentials: true
  })
}

// 用户注册
export function register(data) {
  return request({
    url: '/Users/reg',
    method: 'post',
    data,
    withCredentials: true
  })
}

// 修改密码
export function changePassword(oldPassword, newPassword) {
  return request({
    url: '/Users/changePassword',
    method: 'post',
    params: {
      oldPassword,
      newPassword
    },
    withCredentials: true
  })
}

// ==================== 用户信息管理 ====================

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/Users/getById',
    method: 'get',
    withCredentials: true
  })
}

// 修改用户信息
export function updateUserInfo(data) {
  return request({
    url: '/Users/changeInfo',
    method: 'post',
    data,
    withCredentials: true
  })
}

// 上传头像
export function uploadAvatar(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/Users/upload/avatar',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    withCredentials: true
  })
}

// ==================== 购物车功能 ====================

export function getCartList() {
  return request({
    url: '/Carts/list',
    method: 'get',
    withCredentials: true
  })
}

// ==================== 管理员功能 ====================

// 获取所有用户列表（管理员功能）
export function getAllUsers() {
  return request({
    url: '/Users/all',
    method: 'get',
    withCredentials: true
  })
}

// 删除用户（管理员功能）
export function deleteUser(userId) {
  return request({
    url: `/Users/${userId}`,
    method: 'delete',
    withCredentials: true
  })
} 